<script>
jQuery(function() {
<?php
if($this->onTrigger)
{
    echo "
jQuery('#{$this->onTrigger[0]["element_id"]}').{$this->onTrigger[0]["event_name"]}
(
    function (event)
    {
";
}
$strparam="";
if($this->eventChain)
{
    foreach($this->eventChain as $ind=>$event)
    {
        $strparam=$strparam.".{$event["event_name"]}
(
    {$event["param"]}
)";
    }
}
echo $this->beforeWidgetCode."
";
if($this->optionsString)
    $stroption="{{$this->optionsString}}";
else
    $stroption="";
echo "
if(!jQuery( '#{$this->id}' ))
{
    jQuery( '<div id=\"{$this->id}\">' ).prependTo('body'){$strparam}.{$this->widget}
    (
        {$stroption}
    );
}
else
{
    jQuery( '#{$this->id}' ){$strparam}.{$this->widget}
    (
        {$stroption}
    );
}
";

if(!empty($this->methods)) {
        foreach ($this->methods as $method => $args) {
                print 	'jQuery("#'.$this->id.'").'.$this->widget.'
                    (
                        "'.$method.'" , "'.$args.'"
                    );
';
        }
}			

if(!empty($this->events)) {
        foreach ($this->events as $evnt => $hndl) {
                print 	'jQuery("#'.$this->id.'" ).'.$this->widget.'
                ({
                        '.$evnt.': function( event, ui ) {'.$hndl.'( event, ui );}
                });';
        }
}
echo $this->afterWidgetCode."
";
echo "});";
if($this->onTrigger)
{
    echo "
});";
}
?>
</script>